// std_ex.cpp
//
// Extensions to the std library
//
//
// Copyright (c) 2002-2004 Rob Wiskow
// rob-dev@boxedchaos.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include "stdafx.h"
#include "std_ex.h"
#include <sstream>


namespace stdex
{

	std::string itoa(const int iValue)
	{
		std::ostringstream sOutStream;
		sOutStream << iValue;
		return sOutStream.str();
	}
	std::wstring itoaw(const int iValue)
	{
		std::wostringstream sOutStream;
		sOutStream << iValue;
		return sOutStream.str();
	}


	std::wstring ltoaw(const long lValue)
	{
		std::wostringstream sOutStream;
		sOutStream << lValue;
		return sOutStream.str();
	}
	std::string ltoa(const long lValue)
	{
		std::ostringstream sOutStream;
		sOutStream << lValue;
		return sOutStream.str();
	}


	std::wstring ftoaw(const float fValue)
	{
		std::wostringstream sOutStream;
		sOutStream << fValue;
		return sOutStream.str();
	}
	std::string ftoa(const float fValue)
	{
		std::ostringstream sOutStream;
		sOutStream << fValue;
		return sOutStream.str();
	}


	std::wstring dtoaw(const double dValue)
	{
		std::wostringstream sOutStream;
		sOutStream << dValue;
		return sOutStream.str();
	}
	std::string dtoa(const double dValue)
	{
		std::ostringstream sOutStream;
		sOutStream << dValue;
		return sOutStream.str();
	}


	int atoiw(const std::wstring& sValue)
	{
		int iResult = 0;
		std::wstringstream sTranslation;
		sTranslation << sValue;
		sTranslation >> iResult;
		return iResult;
	}
	int atoi(const std::string& sValue)
	{
		int iResult = 0;
		std::stringstream sTranslation;
		sTranslation << sValue;
		sTranslation >> iResult;
		return iResult;
	}


	long atolw(const std::wstring& sValue)
	{
		long lResult = 0;
		std::wstringstream sTranslation;
		sTranslation << sValue;
		sTranslation >> lResult;
		return lResult;
	}
	long atol(const std::string& sValue)
	{
		long lResult = 0;
		std::stringstream sTranslation;
		sTranslation << sValue;
		sTranslation >> lResult;
		return lResult;
	}


	float atofw(const std::wstring& sValue)
	{
		float fResult = 0.0;
		std::wstringstream sTranslation;
		sTranslation << sValue;
		sTranslation >> fResult;
		return fResult;
	}
	float atof(const std::string& sValue)
	{
		float fResult = 0.0;
		std::stringstream sTranslation;
		sTranslation << sValue;
		sTranslation >> fResult;
		return fResult;
	}



	double atodw(const std::wstring& sValue)
	{
		double dResult = 0.0;
		std::wstringstream sTranslation;
		sTranslation << sValue;
		sTranslation >> dResult;
		return dResult;
	}
	double atod(const std::string& sValue)
	{
		double dResult = 0.0;
		std::stringstream sTranslation;
		sTranslation << sValue;
		sTranslation >> dResult;
		return dResult;
	}


	std::wstring TrimWstring(const std::wstring& sString)
	{
		std::wstring::size_type start = sString.find_first_not_of(L" \t");
		std::wstring::size_type end = sString.find_last_not_of(L" \t");
		std::wstring sResult = L"";
		if (start != std::wstring::npos)
		{
			sResult = sString.substr(start, end - start + 1);
		}

		return sResult;
	}
	std::string TrimString(const std::string& sString)
	{
		std::string::size_type start = sString.find_first_not_of(" \t");
		std::string::size_type end = sString.find_last_not_of(" \t");
		std::string sResult = "";
		if (start != std::string::npos)
		{
			sResult = sString.substr(start, end - start + 1);
		}

		return sResult;
	}


	std::wstring ToWString(const char* sString)
	{
		size_t iStringLength = std::mbstowcs(0, sString, strlen(sString));
		wchar_t* sWString = new wchar_t[iStringLength + 1];
		std::mbstowcs(sWString, sString, iStringLength + 1);
		std::wstring sOutString(sWString);
		delete sWString;
		return sOutString;
	}


	std::string ToMbString(const std::wstring& sWString)
	{
		size_t iStringLength = std::wcstombs(0, sWString.c_str(), sWString.size() * MB_CUR_MAX);
		char* sCharString = new char[iStringLength + 1];
		wcstombs(sCharString, sWString.c_str(), iStringLength + 1);
		std::string sString(sCharString);
		delete sCharString;
		return sString;
	}


	std::vector<std::wstring> DetokenizeWstring(const std::wstring& sString, const std::wstring& sDelimiters)
	{
		std::wstring sStringCopy(sString);
		std::vector<std::wstring> Tokens;

		while (! sStringCopy.empty())
		{
			std::wstring::size_type DelimiterIndex = sStringCopy.find_first_of(sDelimiters);
			if (DelimiterIndex == std::wstring::npos)
			{
				Tokens.push_back(sStringCopy);
				sStringCopy = L"";
			}
			else
			{
				Tokens.push_back(sStringCopy.substr(0, DelimiterIndex));
			}
			sStringCopy = sStringCopy.substr(DelimiterIndex + 1);
		}

		return Tokens;
	}
	std::vector<std::string> DetokenizeString(const std::string& sString, const std::string& sDelimiters)
	{
		std::string sStringCopy(sString);
		std::vector<std::string> Tokens;

		while (! sStringCopy.empty())
		{
			std::string::size_type DelimiterIndex = sStringCopy.find_first_of(sDelimiters);
			if (DelimiterIndex == std::string::npos)
			{
				Tokens.push_back(sStringCopy);
				sStringCopy = "";
			}
			else
			{
				Tokens.push_back(sStringCopy.substr(0, DelimiterIndex));
			}
			sStringCopy = sStringCopy.substr(DelimiterIndex + 1);
		}

		return Tokens;
	}

}
